package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016102500757432";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDKVdNBcmemEkL2bvHnfvMeSkWoMl+Qq9283zad307TEyHDqovxaqccdh2OgqoT8lbqf+uF2e9EF0PhrDOpRMeVMpBC+s30ZcT1qh+T8z1JvJ0OeJRIX3bByLj01SUl2bSgZOIsvA/4v9qsnS5GsZA1t9u0h5Bwq4TLk6mbhGeq0kixwyG6Eb7Zp1G286Uhw9Dx0kAWqQEgxFPDY38ZrGmNDYLSk+dGIEUKSsl/nftxZ24b9ckFgwLNZ7DC8GEZavLmd1fpT0ae/gtpGXZ8wOCRW5+WDDmKxSbhSobufC0p4/G9YmHFwo0d9AscaB8rDEJjLjZxFoacJwjCayv3ZW7jAgMBAAECggEAcuR1Ry6NiJHKEGFDX66IouSN1Up1VZMiIEOnodHR0q2EeLT5LibfqZuwgOHVbeooTJRCywjLgqCrXRQJac6yfhfuLvp3xJWdV9o4gCyY2qwNitARc18x8ivmSrBVnb6XgJSIkdpgP2HB7E58novpVXeHfGWWMURT7b11i+eNbkNjSUkr4ZQGV1W9zriV5CWQkM+GpF23o+i6XVpXIfWzk1LsMGMmwSC6zmPMaXTdsqIyuomHT4/m1Ct1ixiID5e24nqgqM/ofkLbYwrYgRPLNfTmtfWfne5IfgJevkfzXhA0+J5ZbMpS1YUlFyfG1o4C64sohG8J5SenJa8WdzixkQKBgQDwDir7T4JbgFrIv/ZzlE5ZrbDMg2BaYr/kwrZWVMA/i0gEIAJtVc0cfGzOhjw9obXwP+uxpZgkDMPbuwd6goG6scocjPOEB7WyTwaHn3MNfvzBqZGYd39UuuaXvZT8aVTc+CnlCjWrTd5f/KKPvynXUIMXzExKv1KwgFBMu1DwFQKBgQDXxkaYyJWwnqBUKEvDRmo4cX20Uox3PM+ZmQDHVMXu3en8NIUEaeyFsJ3HXz07m0ZqW+gtoMeCOknebImFP7n4a880+xVnt2mht/nXUxmms/z1YCuCItH8t0zbR0Fn4aaEohYzi2lxgVUeC3jpunc+uCb8JAsPfaqrPSZ/KJqpFwKBgAUNF0RweNq6Ca79sw7diQSEDhtPjGB6gXUrz7BjWiQ8JwjHL6XjT0yRJaz20B+v3mcuwSPcyyW/CwP9nHuscVRfV1RgduNJUj5tQULrTP10tZbRnZmqsggzYpTAzXi+MqXvptoF6/kty0cTxsvlhtjb/dYrW1ObUP8bxBTJQDu9AoGAYMRj/rreHtJU1skniRC4LE4BcPAgXI0iESFD7yD+j2xoNhobmWojj6UwbSwyGtw9DQ/gpS3Yq3eWjzCfz+lPbrBywbDk3ZmsvsDyQ/XZqCBKDoUtnfQdtjdCOYjQNVVGR28lg8I+7I9y68F7XV6pgbA2k5daen0CID+RVy+PhMsCgYAndVPBTSuFmfpr0rCARuhvvAspBNTUu3IHQIbN7MiXaiZ8qWUPdjuyPSLMuStFQwMd0HA2YgvY+70nwsNy45FG8xH9LVT92FJnPKw4hYdG4dXt67dD/rpRzRVsEYZfukRnIZN9MlJigysOhF3qNZr3nEfYbpgN8Digb9OdZ4j3ng==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2/8S/Zm/pqpHphqtSKVHJKEDA6x7R9fapbL7JokZ+suupubkoBBFruugum5MnPcNLFG05jGhuGqGLD2rX7hxTc1IinMaxFpi6TFEFi6emnNU7ZO3IazpjT2yluE5YgByIRjmDVptmbgTgpJ6T+qE2uLeJCHcwopLSDlOX4IwXGp8p4yJtuv4umyTKHp8nCQ8qDFCQNiMrCXakm/O/U1Knqsyqke/y+y8LfMbw37Wltq0EWSIVkjhMsRgZjhuWoAXvzxTTaIEQ9FOXfHhXzwMqUUtkwYUGh7od3uCNUXhnhouvV81gYRiQvW2g6skxmtfnoRPF1mca2W0pLUwkRWg/QIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url= "http://m6dgig9awg.52http.tech/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //收单时间
    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
